# 会员信息管理
from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import Member, User
from django.core.paginator import Paginator
from django.db.models import Q  # 或条件
from datetime import datetime
import random


# Create your views here.
def index(request, pIndex=1):
    """ 浏览信息 """
    # umod = User.objects
    ulist = Member.objects.filter(status__lt=9)

    # 搜索条件
    mywhere = []
    kw = request.GET.get('keyword', None)
    if kw:
        ulist = ulist.filter(nickname__contains=kw)
        mywhere.append('keyword=' + kw)

    # 分页处理
    pIndex = int(pIndex)
    page = Paginator(ulist, 5)  # 5条数据一页显示
    maxpages = page.num_pages  # 获取最大页数
    # 判断不能越界
    if pIndex > maxpages:
        pIndex = maxpages
    if pIndex < 1:
        pIndex = 1
    list2 = page.page(pIndex)  # 获取当前页数据
    plist = page.page_range  # 获取页码列表信息
    context = {
        'memberlist': list2,
        'plist': plist,
        'pIndex': pIndex,
        'maxpages': maxpages,
        'mywhere': mywhere
    }
    return render(request, 'myadmin/member/index.html', context)


def delete(request, mid=0):
    """ 删除 """
    print(mid)
    try:
        ob = Member.objects.get(id=mid)
        ob.status = 9
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '删除成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '删除失败'}
    return render(request, 'myadmin/info.html', context)


'''
def add(request):
    """ 添加 """
    return render(request, 'myadmin/user/add.html')


from utils.encrypt import md5
from utils.encrypt import add_salt


def insert(request):
    """ 执行添加 """
    if request.POST['password'] != request.POST['repassword']:
        context = {'info': '两次密码输入不一致'}
        return render(request, 'myadmin/info.html', context)
    try:
        ob = User()
        ob.username = request.POST['username']
        ob.nickname = request.POST['nickname']

        s = request.POST['password'] + str(add_salt)
        ob.password_hash = md5(s)
        ob.password_salt = add_salt
        print(request.POST['password'], s, md5(s))
        ob.status = 1
        ob.create_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '添加成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '添加失败'}

    return render(request, 'myadmin/info.html', context)


def delete(request, uid=0):
    """ 删除 """
    print(uid)
    try:
        ob = User.objects.get(id=uid)
        ob.status = 9
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '删除成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '删除失败'}

    return render(request, 'myadmin/info.html', context)


def edit(request, uid=0):
    """ 编辑 """
    try:
        ob = User.objects.get(id=uid)
        context = {'user': ob}
        return render(request, 'myadmin/user/edit.html', context)

    except Exception as arr:
        print(arr)
        context = {'info': '没有找到要修改的信息'}
        return render(request, 'myadmin/info.html', context)


def update(request, uid=0):
    """ 执行编辑 """
    print(uid)
    try:
        ob = User.objects.get(id=uid)
        ob.status = request.POST['status']
        ob.nickname = request.POST['nickname']
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '修改成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '修改失败'}

    return render(request, 'myadmin/info.html', context)
'''
